﻿using System;
using TYBase;

namespace NaireSystem
{
    public class R<T>
    {
        public bool IsOK()
        {
            return code == 0;
        }
        public int code { get; set; } = 1;

        public string msg { get; set; }

        public T data { get; set; }

        public static R<T> Fail(string message, int code = 1)
        {
            R<T> r = new R<T>();
            r.code = code;
            r.msg = "http:" + message;
            return r;
        }
        public static R<T> Fail(string message)
        {
            R<T> r = new R<T>();
            r.code = 1;
            r.msg = "http:"+message;
            return r;
        }
        public static R<T> Fail(int code)
        {
            R<T> r = new R<T>();
            r.code = code;
            return r;
        }

        public static R<T> Fail(string v, Exception ex)
        {
            R<T> r = new R<T>();
            r.data = default;
            r.code = 1;
            r.msg = $"{v},{ex.Info()}";
            return r;
        }

        public static R<T> OK(T t)
        {
            R<T> r = new R<T>();
            r.data = t;
            r.code = 0;
            return r;
        }
        public static R<T> OK()
        {
            R<T> r = new R<T>(); 
            r.code = 0;
            return r;
        }
    }
}
